package com.mall.common.entity;

import com.mall.common.enums.ResponseEnum;
import com.mall.common.service.IResponseInterface;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author wuyijie
 * @description: TODO 公共返回对象
 * @date 2021/6/20 18:42
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResponseData<T>{

    private int code;

    private String msg;

    private T data;


    public ResponseData(IResponseInterface iResponseInterface) {
        this.code = iResponseInterface.getResponseCode();
        this.msg = iResponseInterface.getResponseMessage();
    }

    public static ResponseData success() {
        return success(null);
    }

    public static ResponseData success(Object t) {
        ResponseData tResponseData = new ResponseData();
        tResponseData.setCode(ResponseEnum.SUCCESS.getResponseCode());
        tResponseData.setMsg(ResponseEnum.SUCCESS.getResponseMessage());
        tResponseData.setData(t);

        return tResponseData;
    }

    public static ResponseData error(IResponseInterface iResponseInterface) {
        ResponseData responseData = new ResponseData();
        responseData.setCode(iResponseInterface.getResponseCode());
        responseData.setMsg(iResponseInterface.getResponseMessage());
        responseData.setData(null);
        return responseData;
    }

    public static ResponseData error(String msg) {
        ResponseData responseData = new ResponseData();
        responseData.setCode(ResponseEnum.INTERNAL_SERVER_ERROR.getResponseCode());
        responseData.setMsg(msg);
        return responseData;
    }

    public static ResponseData error(int code, String msg) {
        ResponseData responseData = new ResponseData();
        responseData.setCode(code);
        responseData.setMsg(msg);
        return responseData;
    }
}
